﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using GameDevelopHelper.Common;

namespace GameDevelopHelper
{
    public partial class frmAddProject : Form
    {
        private string m_strProjectName;
        private string m_strProjectUrl;
        private bool m_bAddSucceed;

        public frmAddProject()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {

            if (txtProjectName.TextLength == 0)
            {
                txtProjectName.Focus();
                return;
            }

            if (txtProjectUrl.TextLength == 0)
            {
                txtProjectUrl.Focus();
                return;
            }

            //判断项目名称是否合法
            string [] strIllegalCharacter = {"/", @"\", "*", "?", "\"", "|", "<", ">"};
            for (int i = 0; i < strIllegalCharacter.Length; i++)
            {
                if (txtProjectName.Text.IndexOf(strIllegalCharacter[i]) > 0)
                {
                    StringTable.ResAlert("PROJECT_NAME_WRONGFUL", StringTable.ALERT_TYPE.WARNING);
                    SelectAll(txtProjectName);
                    return;
                }
            }

            //判断项目URL是否合法
            Regex RegexUrl = new Regex(@"^((https|http|svn)?://)?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?((/?)|(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$");
            if (RegexUrl.IsMatch(txtProjectUrl.Text) == false)
            {
                StringTable.ResAlert("PROJECT_URL_WRONGFUL", StringTable.ALERT_TYPE.WARNING);
                SelectAll(txtProjectUrl);
                return;
            }

            m_strProjectName = txtProjectName.Text;
            m_strProjectUrl = txtProjectUrl.Text;
            m_bAddSucceed = true;

            this.Hide();
        }

        public string GetSelectedProjectName()
        {
            return m_strProjectName;
        }

        public string GetSelectedProjectUrl()
        {
            return m_strProjectUrl;
        }

        public bool GetAddResult()
        {
            return m_bAddSucceed;
        }

        private void frmAddProject_Load(object sender, EventArgs e)
        {
            m_strProjectName = "";
            m_strProjectUrl = "";
            m_bAddSucceed = false;
        }

        //选择文本框的全部文字
        private void SelectAll(TextBox textbox)
        {
            textbox.Focus();
            textbox.SelectAll();
        }
    }
}
